<?php
class Con_file extends Acan{
	public $defaultAct='list';
	public $Conread = '文件管理';
	public $Actread = array(
		'index'=>'无',
		'list'=>'列表',
		'edit'=>'编辑文件',
		'save'=>'保存',
		'savefile'=>'保存文件',
		'resume'=>'恢复备份文件',
		'add'=>'添加文件(夹)',
		'rename'=>'重命名',
		'del'=>'删除文件(夹)',
		'upfile'=>'上传文件',
		'zip'=>'压缩',
		'unzip'=>'解压'
	);
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->afile = Afunc_file();
		$this->_S = Afun_session();
		$this->atitle = $this->Actread[$this->Aact];
		if(Acan_app_name=='web'){
			if($this->_S['aweb']){
				$this->rootpath = '/Anmp/wwwuser/'.$this->_S['aweb']['name'];
			}else{
				return;
			}
		}
	}
	function Act_index(){
		Afun_cache_rm('Adev');
		echo 'del Adev';
		include $this->Atpl();
	}
	function aweb_chk($path){
		if(!empty($this->rootpath) && $this->rootpath != substr($path,0,strlen($this->rootpath))){
			Afun_refresh('非法操作','referrer');
			exit;
		}
	}
	function Act_list(){
		$ziptype = array('zip','7z','tar');
		$edittype = array('txt','log','php','html','htm','xml','js','css','conf','ini');
		$path = realpath(Acan_anmp_path);
		if($this->rootpath){
			$path = $this->rootpath.'/www';
		}
		if($_GET['dir']){
			$path = $_GET['dir'];
		}
		$this->aweb_chk($path);
		$path = str_replace('\\','/',$path);
		if(substr($path,-1,1)!='/'){$path .= "/";}
		if(Afun_get_os()!='Linux'){
			if(preg_match('/^([a-zA-Z]:)/i',$path)==1){$path_h = '';}
		//	$path = preg_replace('/([a-zA-Z]:)/i','',$path);
		}else{$path_h = '/';}
		$history = $this->history('dir',$path);
		$data = $this->afile->ascandiro($path,0,2);
		if($this->id=='list_dir'){
			include $this->Atpl('file/'.$this->id);return;
		}
		if($this->id=='list_file'){
			include $this->Atpl('file/'.$this->id);return;
		}
		$prr = explode('/',$path);
		$dirnav = '';
		foreach ($prr as $v) {
			if(empty($v)){continue;}
			if(empty($pplus) && $path_h==''){$path_s = '';}else{$path_s = '/';}
			$pplus .= $path_s.$v;
			$dirnav .= $path_s.'<a href="'.$this->Aactr.'?dir='.$pplus.'">'.$v.'</a>';
		}
		include $this->Atpl();
	}
	function Act_edit(){
		$path = $_GET['file'];
		$this->aweb_chk($path);
		$charset = $this->id;
		$history = $this->history('file',$path,$charset);
		if(empty($path)){Afun_refresh('请先选择文件','referrer');}
		if($_POST){
			$this->savefile($path,$_POST['note'],$charset);
			Afun_refresh('保存成功','referrer');
		}
		$note = $this->afile->read($path,$charset);
		$note = htmlspecialchars($note);
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		if(!file_exists($path_bak)){$path_bak='';}
		include $this->Atpl();
	}
	function Act_save(){
		$path = $_POST['path'];
		$this->aweb_chk($path);
		$this->savefile($path,$_POST['note'],$_POST['charset']);
		echo '保存成功!';
	}
	//--保存文件
	function savefile($path,$note,$charset){
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		$this->afile->mkdir(dirname($path_bak).'/');
		$this->afile->unlink($path_bak);
		$this->afile->copy($path,$path_bak);
		$this->afile->write($path,str_replace("\r\n","\n",$note),$charset);
	}
	//--恢复备份文件
	function Act_resume(){
		$path = $_GET['path'];
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		if(!file_exists($path_bak)){
			$msg='备份文件不存在';
		}else{
			unlink($path);
			copy($path_bak,$path);
			$msg='还原成功';
		}
		Afun_refresh($msg,'referrer');
	}
	function Act_add(){
		$type = $_POST['type'];
		$path = $_POST['path'];
		$this->aweb_chk($path);
		$newname = $_POST['newname'];
		if($type=='dir'){
			$msg = $this->afile->mkdir($path.$newname);
		}elseif($type=='file'){
			$msg = $this->afile->mkfile($path.$newname,$note);
		}
		echo $msg;
	}
	function Act_rename(){
		$type = $_POST['type'];
		$path = $_POST['path'];
		$this->aweb_chk($path);
		$name = $_POST['name'];
		$newname = $_POST['newname'];
		if(Afun_get_os()!='Linux'){$path = Afun_charset($path,'gbk');$name = Afun_charset($name,'gbk');$newname = Afun_charset($newname,'gbk');}
		if(file_exists($path.$newname)){
			$msg = $path.$newname.'已经存在！';
		}else{
			rename($path.$name,$path.$newname);
			$msg = '重名名成功！';
		}
		echo $msg;
	}
	function Act_del(){
		$path = $_GET['path'];
		$this->aweb_chk($path);
		if(empty($path)){echo '请先选择文件或者文件夹';return;}
		if($this->id=='file'){
			$msg= $this->afile->unlink($path);
		}elseif($this->id=='dir'){
			$msg=$this->afile->rmdir($path);
		}
		echo $msg;
		//Afun_refresh($msg,'referrer');
	}

	function Act_upfile(){
		$path = $_GET['path'];
		$this->aweb_chk($path);
		if($_POST){
			header('Content-type: text/html; charset=UTF-8');
			$path = $_POST['path'];
			$upname = 'file';
			$savepath = Afunc_file()->upfile($upname,$path);
			echo "<script>window.parent.Acan.close('ajax_dev');
				window.parent.Apps.file.ajax_list('file');
				window.parent.Acan.alert('成功上传至 $savepath',1000);</script>";exit;
		}
		include $this->Atpl();
	}
	//--压缩文件/夹
	function Act_zip(){
	//	7z a -tzip -p111 archive.7z txt.txt
		$data = $_POST;
		$tmp;
		if(is_array($data['zip'])){
			foreach ($data['zip'] as $k => $v) {
				$tmp .= ' '.$data['path'].$v;
			}
			$savename = $data[path].$data[name].".".$data[ziptype];
			if(Afun_get_os()!='Linux'){
				$cmd = A_ROOT."../server/tool/7za a -t$data[ziptype] $savename $tmp";
				$cmd = Afun_charset($cmd,'gbk');
			}else{
				$cmd = "7za a -t$data[ziptype] $savename $tmp";
			}
			$cmds = Afun_charset(`$cmd`);
			$endtime=gettime();
			$times = round($endtime- Acan_run_startime,6).'秒';
			echo "<p>压缩耗时:$times</p><p>压缩信息:</p><pre>$cmds</pre>";
		}else{
			echo '未选择文件!';
		}
	}
	//--解压文件/夹
	function Act_unzip(){
	//	7z x -tzip -p111 archive.7z
		$data = $_POST;
		$tmp;
		$name = $data['name'];
		$nrr = explode('.',$name);
		$name = $data['path'].$name;
		$type = $nrr[count($nrr)-1];
		if(!empty($name)){
			if(Afun_get_os()!='Linux'){
				$cmd = A_ROOT."../server/tool/7za x -y -o$data[unpath] -t$type $name";
				$cmd = Afun_charset($cmd,'gbk');
			}else{
				$cmd = "7za x -y -o$data[unpath] -t$type $name";
			}
			$cmds = Afun_charset(`$cmd`);
			$endtime=gettime();
			$times = round($endtime- Acan_run_startime,6).'秒';
			echo "<p>解压耗时:$times</p><p>解压路径:$data[unpath]</p><p>解压信息:</p><pre>$cmds</pre>";
		}else{
			echo '未选择文件!';
		}
	}
	//--压缩优化图片
	function Act_imgop(){
		set_time_limit (0);error_reporting(E_ALL);
		$imk = Afun_acan_new('imagick');
		$path = $_GET['path'];
		if(!is_dir($path)){
			echo '目录错误';
		}
		$data = $this->afile->ascandiro($path,0,2);
		$bakpath = $path.'_bak/';
		if(!file_exists($bakpath)) mkdir($bakpath);
		echo '<table>';
		echo "<tr><td>文件名</td><td>原大小</td><td>压缩后</td><td>节省</td><td>还原</td></tr>";
		$total = 0;
		$optotal = 0;
		$num = 0;
		foreach($data->file as $k=>$v){
			$msg = '';
			if(in_array($v['ext'],array('jpg','png','PNG','JPG'))){
				if($v['size']>100000){
					$num++;
					copy($v['path'],$bakpath.$v['names']);
					$imk->load($v['path']);
					$imk->optimum();
					$imk->load($v['path']);
					$opsize = $imk->size;
					$total += $v['size'];
					$optotal += $opsize;
					if($opsize>$v['size']){
						unlink($v['path']);
						rename($bakpath.$v['names'],$v['path']);
						$msg = '还原';
					}
					echo "<tr><td>$v[path]</td><td>$v[size]</td><td>$imk->size</td><td>".($v['size'] - $opsize)."</td><td>$msg</td></tr>";
				}
			}
		}
		$this->afile->rmdir($bakpath);
		echo "<tr><td>合计:$total</td><td>优化后:$optotal</td><td>节省:".($total-$optotal)."</td><td>共{$num}个文件</td><td></td></tr>";
		echo '</table>';
	}
	//打开历史
	function history($type,$path,$charset='utf8'){
		$num['file'] = 50;
		$num['dir'] = 50;
		$data = Afun_cache_get('Adev:history');
		$cache = $data->$type;
		$datas = $cache->datas;
		$key = count($datas);
		//--检查是否缓存过
		if(isset($cache->keys[$path])){$key = $cache->keys[$path];}
		$datas[$key]['path']=$path;
		$datas[$key]['time']=time();
		$datas[$key]['times']= $datas[$key]['times']+1;
		$datas[$key]['charset']= 'utf8';
		$datas = Afun_arr_sort($datas,'path','ASC');
		//--删除多余的数组
		if(!empty($datas[$num[$type]])){unset($datas[$num[$type]]);}
		//--重新排列key
		if(!empty($cache->keys)){unset($cache->keys);}
		foreach($datas as $k=>$v){
			$cache->keys[$v['path']] = $k;
		}
		$cache->datas = $datas;
		$data->$type = $cache;
		Afun_cache_set('Adev:history',$data);
		return $data;
	}
}
?>